1 00:00:00,560 --> 00:00:01,310 Welcome. 2 00:00:01,310 --> 00:00:06,350 In this lecture, we're going to explore a fundamental programming concept known as variables. 3 00:00:06,380 --> 00:00:13,940 A variable is a container or a labeled storage box that can hold different values like numbers, text, 4 00:00:13,940 --> 00:00:16,100 and more complex data structures. 5 00:00:16,130 --> 00:00:21,710 Variables are used to store and manipulate data within your game, and this data can be retrieved whenever 6 00:00:21,710 --> 00:00:22,340 you'd like. 7 00:00:22,370 --> 00:00:28,880 Variables have an identifier, such as a name and a value associated with that identifier. 8 00:00:28,910 --> 00:00:31,310 A great analogy for a variable is a mailbox. 9 00:00:31,310 --> 00:00:37,280 You can think of a mailbox like a variable because it has an address or an identifier, and that mailbox 10 00:00:37,280 --> 00:00:39,740 can hold different items, which would be your values. 11 00:00:39,740 --> 00:00:45,050 Just like you can put letters, packages, and other items into a mailbox and retrieve them when needed. 12 00:00:45,050 --> 00:00:50,390 In programming, you can store various types of data in a variable and access that data later on by 13 00:00:50,390 --> 00:00:52,340 referring to that variable's name. 14 00:00:52,730 --> 00:00:58,940 So to create a variable, the first thing we need to do is type out the name or identifier for the variable. 15 00:00:58,940 --> 00:01:04,370 In this case, we can call our first variable something like my first var. 16 00:01:04,370 --> 00:01:11,450 This is going to be acting as our identifier or name for the variable, and then to store a value or 17 00:01:11,450 --> 00:01:16,340 associate a value with this identifier, we need to use one equal sign. 18 00:01:16,340 --> 00:01:19,700 The equal sign is known as an assignment operator. 19 00:01:19,700 --> 00:01:24,440 In Lua, we are associating or assigning a value with the identifier. 20 00:01:24,440 --> 00:01:29,480 And as an example, what I'm going to store in this variable is going to be the number like ten. 21 00:01:29,480 --> 00:01:30,770 So now here we are. 22 00:01:30,770 --> 00:01:36,830 We have our variable called my first var and it stores the value of ten somewhere in our computer's 23 00:01:36,830 --> 00:01:37,520 memory. 24 00:01:38,030 --> 00:01:42,530 Now we can store a lot more than just numbers inside of our variables. 25 00:01:42,530 --> 00:01:47,180 By default, Lua has seven basic data types that you can store inside of your variables. 26 00:01:47,180 --> 00:01:54,680 These data types are numbers, strings, booleans, functions, tables, threads, and nil. 27 00:01:54,680 --> 00:01:59,870 Now again, numbers can be anything from negative to positive, whole or decimal. 28 00:01:59,870 --> 00:02:01,070 Any whole numbers. 29 00:02:01,070 --> 00:02:02,930 We like to call them integers. 30 00:02:02,930 --> 00:02:04,310 So as an example ten. 31 00:02:04,310 --> 00:02:05,870 Here would be an integer. 32 00:02:05,870 --> 00:02:11,690 And any numbers that have decimals are called floating point numbers or float for short. 33 00:02:11,690 --> 00:02:17,750 So for example, if I put a dot here and then put a zero, this would now be considered a floating point 34 00:02:17,780 --> 00:02:18,410 number. 35 00:02:18,410 --> 00:02:23,300 The reason we call them floating point numbers is because the decimal point that describes the fraction 36 00:02:23,300 --> 00:02:28,700 of the number is able to float around or change to represent a different number. 37 00:02:28,700 --> 00:02:37,130 For example, if we had a number like 3.14, this could be changed into something like 31.4, or we 38 00:02:37,130 --> 00:02:43,490 could do 0.31 for the decimal point can shift or float around. 39 00:02:43,490 --> 00:02:48,980 Now strings like we talked about previously are any text between quotation marks. 40 00:02:48,980 --> 00:02:53,660 We can use strings to do things like share messages to players, make announcements in our game, run 41 00:02:53,660 --> 00:02:55,730 a countdown timer, and much more. 42 00:02:55,730 --> 00:02:59,840 So whenever you want to work with text in your scripts, you need to place that text between quotation 43 00:02:59,840 --> 00:03:01,340 marks to denote it as a string. 44 00:03:01,340 --> 00:03:02,000 Data type. 45 00:03:02,000 --> 00:03:07,010 So if I wanted to say something like hello, I would put my quotation marks here and type in hello. 46 00:03:07,010 --> 00:03:10,280 And now my variable is storing a string data type. 47 00:03:10,760 --> 00:03:12,380 Next is a boolean. 48 00:03:12,380 --> 00:03:17,780 And a boolean is a very simple data type that represents two possible values, either true or false. 49 00:03:17,780 --> 00:03:18,950 Boolean values. 50 00:03:18,950 --> 00:03:22,670 Let our computers make decisions based on a condition. 51 00:03:22,670 --> 00:03:28,100 For example, we can check if a player has VIP and if they do have VIP, we can give them some extra 52 00:03:28,100 --> 00:03:30,530 goodies associated with that VIP. 53 00:03:30,530 --> 00:03:37,280 So in my first var, I can store a value like false by just typing out false or typing out something 54 00:03:37,280 --> 00:03:38,120 like true. 55 00:03:38,120 --> 00:03:40,190 That is our boolean data type. 56 00:03:40,520 --> 00:03:46,160 Now functions which we will go over later are stored in variables as well. 57 00:03:46,160 --> 00:03:51,830 They have an identifier and you can refer to this identifier and basically call the function to have 58 00:03:51,830 --> 00:03:52,460 it execute. 59 00:03:52,460 --> 00:03:56,240 Code functions are simply blocks of code that perform a specific task. 60 00:03:56,240 --> 00:03:59,750 And as I said, we will take a look at them more closely later. 61 00:04:00,110 --> 00:04:05,600 Now a table is another data type and they are created by using curly brackets. 62 00:04:05,600 --> 00:04:13,070 So up here if I put two curly brackets I have now created a table data type and this table is currently 63 00:04:13,070 --> 00:04:13,850 empty. 64 00:04:13,850 --> 00:04:19,550 Tables can be used to store many different values and they can even represent different data structures. 65 00:04:19,550 --> 00:04:24,740 We will be going over tables later in the course, so I wouldn't worry too much about them for now. 66 00:04:24,740 --> 00:04:31,880 The next data type is known as a thread, and a thread is created by something in Lua called a coroutine. 67 00:04:31,880 --> 00:04:32,660 Once more. 68 00:04:32,660 --> 00:04:35,510 We do not need to worry about this data type for now either. 69 00:04:35,510 --> 00:04:39,920 The last, but definitely not the least data type is nil. 70 00:04:39,920 --> 00:04:47,120 This is a special value that represents the absence of a value or the concept of nothing in programming, 71 00:04:47,120 --> 00:04:52,910 so we use Nil to indicate that a variable does not currently have any assigned value. 72 00:04:52,910 --> 00:04:59,900 So up here, if I set my first var equal to nil, I'm basically saying that my first var has no. 73 00:05:00,040 --> 00:05:00,820 Value at all. 74 00:05:00,820 --> 00:05:06,160 It hasn't been initialized with anything, so nil represents the absence of a value. 75 00:05:06,460 --> 00:05:11,440 Now the next thing I'm going to quickly note is that you may be wondering why all of this text inside 76 00:05:11,440 --> 00:05:13,750 of my scripts is grayed out. 77 00:05:13,750 --> 00:05:20,830 And this is because these are comments, which is for me and other people to read and not the computer. 78 00:05:20,830 --> 00:05:26,080 If you want to make a comment, you can do so very easily by just putting two dashes before any text. 79 00:05:26,080 --> 00:05:31,180 So right here, when I type out something like hello there, the interpreter is trying to read this 80 00:05:31,180 --> 00:05:31,810 as code. 81 00:05:31,810 --> 00:05:34,270 And because this is not code, we're getting an error here. 82 00:05:34,270 --> 00:05:39,850 But if I put two dashes before it now I have created a comment and this is just there to help you make 83 00:05:39,850 --> 00:05:41,140 notes inside of your code. 84 00:05:41,140 --> 00:05:44,500 Maybe there's an area that might be a little confusing and you want to clarify. 85 00:05:44,500 --> 00:05:47,350 You put comments in there to make it easier to understand. 86 00:05:47,350 --> 00:05:51,340 We are also able to make multi-line comments as shown right here. 87 00:05:51,340 --> 00:05:58,240 And the way to make a multi-line comment is you first go, you put two dashes and then you go ahead 88 00:05:58,240 --> 00:06:00,250 and put two of these brackets. 89 00:06:00,250 --> 00:06:02,680 And this creates a multi-line comment. 90 00:06:02,680 --> 00:06:05,320 We can also do the same thing with strings. 91 00:06:05,320 --> 00:06:09,520 So if you don't want to use quotations you can use those two brackets. 92 00:06:09,520 --> 00:06:14,320 And now we can create a string that expands over multiple different lines. 93 00:06:14,320 --> 00:06:19,150 So this is a multi-line string. 94 00:06:19,330 --> 00:06:24,910 So if we try to do this with quotation marks it's obviously not going to work. 95 00:06:24,910 --> 00:06:30,490 As you can see we're getting a bunch of red underlines here because we have to do it with two brackets 96 00:06:30,490 --> 00:06:32,980 for multi-line strings. 97 00:06:33,370 --> 00:06:38,500 Now another thing I want to mention with variables are the names that we have given them. 98 00:06:38,500 --> 00:06:43,390 You want to make sure that the names you give to your variables are descriptive, so you can understand 99 00:06:43,390 --> 00:06:46,450 what the value inside of them is supposed to represent. 100 00:06:46,450 --> 00:06:51,970 Traditionally, we name variables using the camel case naming convention. 101 00:06:51,970 --> 00:06:57,700 This means that the first letter in the first word of your variable is lowercase, while every other 102 00:06:57,700 --> 00:07:00,700 word after your first word starts with an uppercase letter. 103 00:07:00,700 --> 00:07:03,190 This is the camel case naming convention. 104 00:07:03,220 --> 00:07:07,090 Now, you can also not name your variables starting with numbers. 105 00:07:07,090 --> 00:07:12,730 So if I try to put a one here, we're going to get an error because we cannot name our variables starting 106 00:07:12,730 --> 00:07:13,720 with numbers. 107 00:07:13,720 --> 00:07:18,790 We also cannot name our variables with the keywords that are reserved in Lua. 108 00:07:18,790 --> 00:07:26,770 So for example, one keyword that is reserved in Lua would be something like if and. 109 00:07:26,770 --> 00:07:32,110 As you can see, we're getting a big red error here because we cannot override this default keyword 110 00:07:32,110 --> 00:07:33,790 in Lua as a variable. 111 00:07:33,790 --> 00:07:38,680 If we made it into something like if like or something like that, then it works just fine. 112 00:07:38,680 --> 00:07:41,830 But we cannot override the defaults. 113 00:07:41,890 --> 00:07:47,440 Uh, but we cannot override the default keywords that are in the Lua language. 114 00:07:47,440 --> 00:07:53,590 Now, you might be wondering why do we name our variables with this weird camel case? 115 00:07:53,590 --> 00:07:59,590 Well, the reason we do this is to differentiate things from being a variable, a class, or whatever 116 00:07:59,590 --> 00:08:05,740 else camel case is a commonly used naming convention in Lua programming, and some other commonly used 117 00:08:05,740 --> 00:08:09,280 naming conventions is something known as snake case. 118 00:08:09,370 --> 00:08:13,960 So if I go down here, we have different other naming conventions that are commonly used. 119 00:08:13,960 --> 00:08:17,140 So here is our camel case my first var. 120 00:08:17,140 --> 00:08:24,400 There's also something known as snake case which is popularly used in the Python programming languages. 121 00:08:24,400 --> 00:08:27,130 And then we also have something known as Pascal case. 122 00:08:27,130 --> 00:08:31,090 So you can see the differences between these three different naming conventions. 123 00:08:31,090 --> 00:08:34,390 Pascal case has the first letter capitalized. 124 00:08:34,390 --> 00:08:40,240 Snake case has them all lowercase with an underscore separating them, and then camel case starts with 125 00:08:40,240 --> 00:08:44,980 a lowercase and continues on with capitalized letters. 126 00:08:45,250 --> 00:08:52,120 We typically use Pascal case when we give variable names for classes or something called enums. 127 00:08:52,120 --> 00:08:55,930 Snake case basically has the same principle as camel case. 128 00:08:55,930 --> 00:09:01,390 We use them for variables and stuff like that, but most commonly you'll be using camel case in Lua. 129 00:09:01,390 --> 00:09:07,030 We can also use snake case in another way by capitalizing all of the letters in snake case. 130 00:09:07,030 --> 00:09:14,590 So if we do something like this, my first var, then this variable name right here is actually representing 131 00:09:14,590 --> 00:09:19,810 a constant or a value that should never change during the runtime of your game. 132 00:09:19,810 --> 00:09:24,760 So if you want to define some particular constants in your game or values you never want to change, 133 00:09:24,760 --> 00:09:30,700 then you would want to do it by using capitals and then underscores to separate the different words 134 00:09:30,700 --> 00:09:31,570 from each other. 135 00:09:32,190 --> 00:09:38,010 I'm also going to scroll up here and give you another look at some valid and invalid variable names. 136 00:09:38,010 --> 00:09:42,030 So again, we cannot name our variables starting with numbers. 137 00:09:42,030 --> 00:09:45,360 We cannot use any of the reserved keywords in Lua. 138 00:09:45,450 --> 00:09:49,560 And we can also not use special characters that are reserved in Lua as well. 139 00:09:49,560 --> 00:09:54,780 We can't use exclamation marks, we can't use multiplication symbols or the and symbol, stuff like 140 00:09:54,780 --> 00:09:54,960 that. 141 00:09:54,960 --> 00:10:00,840 These are invalid variable names that are not going to work, but you can go ahead and use these variable 142 00:10:00,840 --> 00:10:01,290 names. 143 00:10:01,290 --> 00:10:03,510 These work just fine. 144 00:10:03,960 --> 00:10:09,120 Now one last thing I want to mention is that variable names are case sensitive in Lua, meaning that 145 00:10:09,120 --> 00:10:15,780 I could have two variables with the exact same words like these ones here below. 146 00:10:15,780 --> 00:10:21,240 But I can have different capitalization between these two names. 147 00:10:21,240 --> 00:10:26,520 And because there's different capitalization, that actually means these two variable names are completely 148 00:10:26,520 --> 00:10:27,150 different. 149 00:10:27,150 --> 00:10:31,440 These two variables would reference different places in memory, and they're not the same. 150 00:10:31,440 --> 00:10:34,800 So you need to keep in mind that Lua is case sensitive. 151 00:10:34,800 --> 00:10:39,900 So you got to make sure that when you are typing out or referencing different variables or properties, 152 00:10:39,900 --> 00:10:42,510 that the case matches perfectly. 153 00:10:42,510 --> 00:10:45,270 Otherwise you're going to run into some problems. 154 00:10:45,780 --> 00:10:49,560 The most important thing to understand from this lecture is to be consistent with your code. 155 00:10:49,560 --> 00:10:56,610 Ensure that you are following a naming convention consistently and you don't want to do something like 156 00:10:56,610 --> 00:11:00,060 type out variable names with multiple different naming conventions. 157 00:11:00,060 --> 00:11:05,340 So as an example, if you were typing out a bunch of variable names for things in your scripts, don't 158 00:11:05,340 --> 00:11:10,530 use multiple different types of naming conventions and stuff like that, because that's extremely ugly 159 00:11:10,530 --> 00:11:12,780 and it makes your code difficult to read. 160 00:11:12,780 --> 00:11:18,090 So as you can see here, if we did something like this and we try to read all the different variable 161 00:11:18,090 --> 00:11:20,040 names, it's very inconsistent. 162 00:11:20,040 --> 00:11:22,470 None of them are using the same naming convention. 163 00:11:22,470 --> 00:11:26,400 Your eyes have to switch around between how the words are placed together. 164 00:11:26,400 --> 00:11:27,510 It's just it's awful. 165 00:11:27,510 --> 00:11:28,020 It's ugly. 166 00:11:28,020 --> 00:11:29,190 Don't ever do this. 167 00:11:29,190 --> 00:11:35,070 Another thing that I've commonly seen done when people are creating their variables is they omit or 168 00:11:35,070 --> 00:11:41,100 remove the spaces between the assignment operator and the value they are assigning to their variable. 169 00:11:41,100 --> 00:11:45,150 So I've seen people do something like this again, don't do this. 170 00:11:45,150 --> 00:11:46,470 This is very ugly. 171 00:11:46,470 --> 00:11:51,540 It's like you were writing an essay, but you decided to leave out all the spaces between the words. 172 00:11:51,540 --> 00:11:53,610 It's ugly, it's hard to read and it just looks dumb. 173 00:11:53,610 --> 00:11:54,720 So don't ever do that. 174 00:11:54,990 --> 00:11:59,070 Okay, so let's go ahead and start practicing using some variables. 175 00:11:59,070 --> 00:12:01,260 So I'm going to create a few variables here. 176 00:12:01,260 --> 00:12:05,880 And you can go ahead and follow along with me I'm going to call this variable something like quote of 177 00:12:05,880 --> 00:12:06,750 the day. 178 00:12:06,750 --> 00:12:09,420 Again we're using the camel case naming convention. 179 00:12:09,420 --> 00:12:11,640 We're going to use our assignment operator. 180 00:12:11,640 --> 00:12:14,940 And we're going to assign a string value to this. 181 00:12:14,940 --> 00:12:19,860 And the quote of the day can be something like, don't be a lazy coder. 182 00:12:20,310 --> 00:12:20,910 There we go. 183 00:12:20,910 --> 00:12:22,530 So now we have a variable. 184 00:12:22,530 --> 00:12:25,380 And this variable is storing a string data type. 185 00:12:25,380 --> 00:12:27,600 We can go ahead and create another variable. 186 00:12:27,600 --> 00:12:31,740 I'm just going to call this one a Boolean example. 187 00:12:32,480 --> 00:12:35,480 And inside of here we're just going to store a true value. 188 00:12:35,480 --> 00:12:36,110 So there we go. 189 00:12:36,110 --> 00:12:39,590 We have a variable storing a boolean data type. 190 00:12:39,980 --> 00:12:41,990 We're going to go ahead and create another variable. 191 00:12:41,990 --> 00:12:44,360 I'm going to call this one number example. 192 00:12:44,360 --> 00:12:50,120 And inside of here we can store a floating point number like 1.923. 193 00:12:50,630 --> 00:12:53,420 And then we can go ahead and create another variable. 194 00:12:53,420 --> 00:12:56,450 I'm going to call this one nil example. 195 00:12:56,720 --> 00:13:00,110 And we're just going to set this equal to nil. 196 00:13:00,110 --> 00:13:01,160 So there we go. 197 00:13:01,190 --> 00:13:06,530 Now the very cool thing is we can reference these different variables by referring to their names or 198 00:13:06,530 --> 00:13:07,640 their identifiers. 199 00:13:07,640 --> 00:13:12,860 So what we're going to do is we're going to use our print function and we're going to pass the name 200 00:13:12,860 --> 00:13:14,210 of these different variables. 201 00:13:14,210 --> 00:13:16,790 So for example here I can type out quote of the day. 202 00:13:17,180 --> 00:13:19,730 And then I can hit tab to autofill that right there. 203 00:13:19,910 --> 00:13:21,800 And then we can use the print function again. 204 00:13:21,800 --> 00:13:24,170 And we can print out our Boolean example. 205 00:13:24,170 --> 00:13:30,290 We can print out our number example and we can print out our nil example. 206 00:13:30,950 --> 00:13:35,840 Now what this is going to do is it's not going to print out the name of the variable, but instead it's 207 00:13:35,840 --> 00:13:38,090 going to look at this variable. 208 00:13:38,090 --> 00:13:44,090 It's going to find whatever is stored in memory, which in this case would be our string or this one 209 00:13:44,090 --> 00:13:45,320 would be the boolean, whatever. 210 00:13:45,320 --> 00:13:49,970 It would take that value and it's going to print it out into our output or console. 211 00:13:49,970 --> 00:13:55,430 If you do not have your output or console open, make sure you head up to the view tab and enable the 212 00:13:55,430 --> 00:13:57,230 output button, which is right here. 213 00:13:58,110 --> 00:14:02,760 So now we can go ahead and see these values get printed out in our console by going to our test tab, 214 00:14:02,760 --> 00:14:06,390 hitting this little drop down arrow here and pressing run. 215 00:14:07,510 --> 00:14:11,620 And now, as you can see, we get the different values printed inside of our console. 216 00:14:11,620 --> 00:14:12,520 So we get our message. 217 00:14:12,520 --> 00:14:13,690 Don't be a lazy coder. 218 00:14:13,690 --> 00:14:19,090 We get true printed out, we get our number printed out, and then we get nil printed out so we can 219 00:14:19,090 --> 00:14:20,110 go ahead and end the test. 220 00:14:20,110 --> 00:14:26,170 And one more thing I would like to show you is that we can also update the values inside of our variables. 221 00:14:26,170 --> 00:14:32,260 So when we instantiate or create this variable, it's not going to be stuck with a string forever. 222 00:14:32,260 --> 00:14:36,280 We can easily change the value stored inside of this variable to whatever we'd like. 223 00:14:36,280 --> 00:14:40,720 So for example, I can refer to quote of the day. 224 00:14:41,620 --> 00:14:46,570 And then to override the value inside of here, I'm just going to use the assignment operator once more. 225 00:14:46,570 --> 00:14:49,090 And inside of here I can change it to be something like a number. 226 00:14:49,090 --> 00:14:51,400 So we can put in something like 100. 227 00:14:51,400 --> 00:14:58,240 And now when our game is going to execute, it should print out 100 here and not our string because 228 00:14:58,240 --> 00:15:03,760 we first create the string and then later it gets overridden with the value of 100. 229 00:15:03,760 --> 00:15:06,880 So if we go back to the test tab and hit run. 230 00:15:07,950 --> 00:15:12,300 As you can see, our message is gone and now we have 100 printed into the console. 231 00:15:12,930 --> 00:15:19,140 This perfectly demonstrates how Lua is a serially executed programming language. 232 00:15:19,140 --> 00:15:20,220 What does that mean? 233 00:15:20,220 --> 00:15:27,480 Well, it means that Lua code is executed line by line, starting at the top and going down each line 234 00:15:27,480 --> 00:15:29,370 until it reaches the bottom of your script. 235 00:15:29,370 --> 00:15:34,470 So when our game is run, the interpreter goes into our script and it starts executing code. 236 00:15:34,470 --> 00:15:37,650 It goes to the first line of code, which would be here. 237 00:15:37,650 --> 00:15:41,370 It would be like, okay, I need to create a variable with this value inside of it. 238 00:15:41,370 --> 00:15:43,380 So it stores that information in memory. 239 00:15:43,380 --> 00:15:46,260 It goes to the next line oh, I got to do the same thing. 240 00:15:46,260 --> 00:15:50,790 Creates another variable, stores that value in memory and it keeps going down. 241 00:15:50,790 --> 00:15:54,240 And then eventually it's going to hit this line right here. 242 00:15:54,240 --> 00:15:59,760 And it's going to see, oh we're referring to a variable that we've already created previously. 243 00:15:59,760 --> 00:16:04,650 And we want to override the value stored inside of that variable with this new value. 244 00:16:04,650 --> 00:16:05,820 So it does that. 245 00:16:05,820 --> 00:16:09,510 It removes our string and replaces it with this number. 246 00:16:09,510 --> 00:16:14,730 And then finally when we get down to the different print functions it prints out those values inside 247 00:16:14,730 --> 00:16:15,630 of our console. 248 00:16:15,630 --> 00:16:23,340 In this case that would be 100 true 1.923 and then nil. 249 00:16:24,710 --> 00:16:29,150 So congratulations, you've reached the end of the first Lua basics lecture. 250 00:16:29,150 --> 00:16:36,200 This was a lot of information to digest, but you're going to be using variables all of the time, so 251 00:16:36,200 --> 00:16:40,130 it's very important you understand and get comfortable using them. 252 00:16:40,130 --> 00:16:42,320 See you in the next lecture.